<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">

      <!-- 登录表单 -->
      <el-form ref="loginForm" size="large" autocomplete="off" :model="formModel" :rules="rules">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" :prefix-icon="User" v-model="formModel.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="请输入密码" :prefix-icon="Lock"
                    v-model="formModel.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="button" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script setup>
import {ref, onMounted} from 'vue'
import {User, Lock, Back, Right} from '@element-plus/icons-vue'
import {getRouters, userLoginService} from "@/api/sys/manager.js";
import {serverManager, serverToken} from '@/stores'
import {useRouter} from 'vue-router'
import {ElMessage} from "element-plus";
import {useRequest} from "vue-request";
import {addServerRoutes, resetRouter,serverMenus} from "@/router/index.js";

const router = useRouter()


const isRegister = ref(false)
const loginForm = ref()

// 整个的用于提交的form数据对象
const formModel = ref({
  username: 'admin',
  password: 'admin'
})
const rules = {
  username: [
    {required: true, message: '请输入账号', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
  ]
}

//登录
const {runAsync: loginRun} = useRequest(({username, password}) => userLoginService({username, password}), {manual: true})
const {runAsync: menu} = useRequest(() => getRouters(), {manual: true})


// 登录方法 表单验证
async function login() {

  await loginForm.value.validate()
  const loginRes = await loginRun(formModel.value)

  if (loginRes.data.code === 200) {

    //重置路由、token、菜单、权限、人员信息
    resetRouter()
   
    //保存token和用户信息
    serverManager.value = loginRes.data.data.user
    serverToken.value = loginRes.data.data.tokenValue


    const menuRes = await menu()
    if (menuRes) {
      //存储路由
      addServerRoutes(menuRes.data.data.routeList)

      //存储菜单数据
      serverMenus.value = menuRes.data.data.menus

      router.push('/')
    }
    ElMessage.success('登录成功')
  }
}

onMounted(
    ()=>{
      //重置路由、token、菜单、权限、人员信息
      resetRouter()
    }
)

</script>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;

  .bg {
    background: url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
    url('@/assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    //user-select: none;这个属性用于控制用户是否可以选择元素中的文本。
    //none值意味着用户不能选择文本，
    //这通常用于防止用户复制或拖动页面上的文本。
    user-select: none;

    .title {
      margin: 0 auto;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>